package model.graphic.objects;

import controller.Constants;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import model.graphic.locations.AbsoluteLocation;
import model.graphic.locations.FlexibleRelativeLocation;
import model.graphic.locations.Location;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Jelle, modified by Ben
 */
public class GTriggerConnection extends GConnection {

    private GDispatcherPort dispatcher;
    private GTrigger callable;
    private Constants cons = new Constants();

    public GTriggerConnection() {
    }

    public GTriggerConnection(GDispatcherPort dispatcher, GTrigger callable) {
        super(dispatcher.getUpperRight(), new FlexibleRelativeLocation(((RectangleAttachment) callable.getBase()).location, 0, 0));
        this.callable = callable;
        this.dispatcher = dispatcher;
    }

    @Override
    public String getString() {
        return Constants.getTriggerconnectionString();
    }

    protected Location calcDeleteLocation() {
        int x = 0, y = 0;
        source=dispatcher.getUpperRight();
        target=new FlexibleRelativeLocation(((RectangleAttachment) callable.getBase()).location, 0, 0);
        if (source.getX() > target.getX()) {
            x = target.getX() + (source.getX() - target.getX()) / 2;
        } else {
            x = source.getX() + (target.getX() - source.getX()) / 2;
        }
        if (target.getY() > source.getY()) {
            y = source.getY() + 15 / 2 + (target.getY() - source.getY()) / 2;
        } else {
            y = target.getY() + 15 / 2 + (source.getY() - target.getY()) / 2;
        }
        return new AbsoluteLocation(x, y);
    }

    @Override
    public void paint(Graphics2D g) {
        Stroke oldStroke = g.getStroke();
        Color oldColor = g.getColor();
        Location center = calcDeleteLocation();

        if (this.isVisible()) {
            line.setLine(source.getX(), source.getY() + 15 / 2, target.getX() - callable.getRadius(), target.getY() + 15 / 2);

            if (highlighted || selected) {
                g.setColor(Color.RED);
                g.setStroke(new BasicStroke(2.0f));
                g.setStroke(cons.getDashedBoldStroke());
            } else {
                g.setStroke(cons.getDashedStroke());
                g.setColor(Color.BLACK);
            }

            if (callable.isVisible()) {
                getLine().paint(g);//g.draw(getLine());
            }
            int number = ((GraphicView)this.getParent()).getCurrentTriggerPosIncr();
            g.drawString(Integer.toString(number), center.getX(), center.getY());
        }
        g.setColor(oldColor);
        g.setStroke(oldStroke);

    }

    /**
     * @return the dispatcher
     */
    public GDispatcherPort getDispatcher() {
        return dispatcher;
    }

    /**
     * @param dispatcher the dispatcher to set
     */
    public void setDispatcher(GDispatcherPort dispatcher) {
        this.dispatcher = dispatcher;
    }

    /**
     * @return the callable
     */
    public GTrigger getTrigger() {
        return callable;
    }

    /**
     * @param callable the callable to set
     */
    public void setTrigger(GTrigger callable) {
        this.callable = callable;
    }
}
